package com.bbm.bbmanage.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bbm.bbmanage.entity.AppHttpCodeEnum;
import com.bbm.bbmanage.entity.House;
import com.bbm.bbmanage.entity.HouseStar;
import com.bbm.bbmanage.entity.vo.HouseStarVo;
import com.bbm.bbmanage.entity.vo.ResponseResult;
import com.bbm.bbmanage.mapper.HouseStarMapper;
import com.bbm.bbmanage.service.HouseService;
import com.bbm.bbmanage.service.HouseStarService;
import com.bbm.bbmanage.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (HouseStar)表服务实现类
 *
 * @author lionkliu
 * @since 2024-03-24 14:32:06
 */
@Service("houseStarService")
public class HouseStarServiceImpl extends ServiceImpl<HouseStarMapper, HouseStar> implements HouseStarService {
    @Autowired
    private HouseService houseService;

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        return ResponseResult.okResult(page(new Page<>(pageNum, pageSize)));
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(HouseStar houseStar) {
        return save(houseStar) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(HouseStar houseStar) {
        UpdateWrapper<HouseStar> updateWrapper = new UpdateWrapper();
        // TODO：
        return update(updateWrapper) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }

    @Override
    public ResponseResult getStarUserId(Long userId) {
        LambdaQueryWrapper<HouseStar> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HouseStar::getUserId, userId)
                .orderByDesc(HouseStar::getCreateTime);
        List<HouseStar> list = list(queryWrapper);
        List<HouseStarVo> houseStarVos = BeanCopyUtils.copyBeanList(list, HouseStarVo.class);
        List<HouseStarVo> collect = houseStarVos.stream().map(houseStarVo -> {
            House house = houseService.getById(houseStarVo.getHouseId());
            if (!Objects.isNull(house)) {
                houseStarVo.setImg(house.getImg());
                houseStarVo.setDes(house.getDes());
                houseStarVo.setStars(house.getStars());
                houseStarVo.setPrice(house.getPrice());
                houseStarVo.setTitle(house.getTitle());
                houseStarVo.setAddress(house.getAddress());
            }
            return houseStarVo;
        }).collect(Collectors.toList());

        return ResponseResult.okResult(collect);
    }

    @Override
    public ResponseResult isStar(Integer userId, Long houseId) {
        LambdaQueryWrapper<HouseStar> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HouseStar::getUserId, userId)
                .eq(HouseStar::getHouseId, houseId);
        HouseStar houseStar = getOne(queryWrapper);
        if (!Objects.isNull(houseStar)) {
            return ResponseResult.okResult(200, "用户已收藏");
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_STAR);
        }
    }

    @Override
    public ResponseResult delete(Integer userId, Long houseId) {
        LambdaQueryWrapper<HouseStar> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HouseStar::getUserId, userId)
                .eq(HouseStar::getHouseId, houseId);
        remove(queryWrapper);
        return ResponseResult.okResult();
    }

}

